<?php

/**
 * Maikuro Framework
 *
 * LICENSE:
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * The license is bundled with this package in the file LICENSE.txt.
 * You may also obtain a copy of the License at this URL:
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 * @copyright  Copyright (c) 2010, Jonathan Hedrén
 * @author     Jonathan Hedrén <jonathan.hedren@gmail.com>
 * @license    http://www.apache.org/licenses/LICENSE-2.0 Apache License, Version 2.0
 */

/**
 * A simple translator class.
 */
class Translator
{
	/**
     * The translations. Must be an associative array with the keys as the
     * string to be translated and the value as the translation.
     *
	 * @var array
	 */
	protected static $translations = array();

    /**
     * Sets the translations. The translations-array must be an associative
     * array with the keys as the string to be translated and the value as the
     * translation.
     *
     * @param array $translations
     * @return void
     */
    public static function setTranslations(array $translations)
    {
        self::$translations = $translations;
    }

	/**
     * Returns the translated string or the given string if no translation is
     * found.
	 *
	 * @param string $string
	 * @return string
	 */
	protected static function getTranslation($string)
	{
		if (isset(self::$translations[$string])) {
            return self::$translations[$string];
    	}
		return $string;
	}

    /**
     * Translates a string. Supports string formatting via the sprintf function.
     * Example: Translator::translate('My %s value', 'secret');
     *
     * @param string $string
     * @return string
     */
    public static function translate($string)
    {
		$args = func_get_args();
		$translation = self::getTranslation($string);
		if (count($args) > 1) {
			return call_user_func_array("sprintf", $args);
		}
		return $translation;
    }
}